package model.handlers;

import database.Database;
import java.sql.SQLException;
import model.Product;
import model.ProductFamily;

/**
 *
 * @author Sonny Sandberg
 */
public abstract class ProductHandler 
{
    /**
     *  CREATE OR REPLACE VIEW getFullProductList AS
        (
                SELECT 
                        p.id AS productID, 
                        p.name AS productName,
                        purchasing_price AS purchasePrice,
                        sales_price AS salesPrice,
                        szd AS productSize,
                        has_decoration AS hasDecoration,
                        remain_after_sale AS serviceProduct,
                        active,
                        sold,
                        pf.id AS productFamilyID,
                        pf.name AS productFamilyName
                FROM 
                        bs_product p
                                INNER JOIN bs_product_family AS pf ON pf.id = p.fk_product_family
        );
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static Product[] getFullProductList(String where, String orderby, String limit) throws ClassNotFoundException, SQLException
    {
        Product[] products = null;
        
        Database db = new Database();
        
        String[][] result = db.getFromDB("getFullProductList", where, orderby, limit, 
                "productID", "productName", "purchasePrice", "salesPrice", "productSize", 
                "hasDecoration", "serviceProduct", "active", "sold", 
                "productFamilyID", "productFamilyName");
        products = new Product[result.length];
        
        db.closeDB();
        
        for (int i = 0; i < result.length; i++)
        {
            products[i] = new Product(
                            Integer.parseInt(result[i][0]), result[i][1], result[i][4], Double.parseDouble(result[i][2]), Double.parseDouble(result[i][3]), 
                            stringToBoolean(result[i][5]), stringToBoolean(result[i][6]), stringToBoolean(result[i][7]), 
                            stringToBoolean(result[i][8]), new ProductFamily(Integer.parseInt(result[i][9]), result[i][10]));            
        }
        
        return products;
    }
    
    /**
     *  CREATE OR REPLACE VIEW getFullProductList AS
        (
                SELECT 
                        p.id AS productID, 
                        p.name AS productName,
                        purchasing_price AS purchasePrice,
                        sales_price AS salesPrice,
                        szd AS productSize,
                        has_decoration AS hasDecoration,
                        remain_after_sale AS serviceProduct,
                        active,
                        sold,
                        pf.id AS productFamilyID,
                        pf.name AS productFamilyName
                FROM 
                        bs_product p
                                INNER JOIN bs_product_family AS pf ON pf.id = p.fk_product_family
        );
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static Product[] getProductArrayByString(String where, String orderby, String limit) throws ClassNotFoundException, SQLException
    {
        Product[] products = null;
        
        Database db = new Database();
        
        String[][] result = db.getFromDB("getFullProductList", where, orderby, limit, 
                "productID", "productName", "purchasePrice", "salesPrice", "productSize", 
                "hasDecoration", "serviceProduct", "active", "sold", 
                "productFamilyID", "productFamilyName");
        products = new Product[result.length];
        
        db.closeDB();
        
        for (int i = 0; i < result.length; i++)
        {
            products[i] = new Product(
                            Integer.parseInt(result[i][0]), result[i][1], result[i][4], Double.parseDouble(result[i][2]), Double.parseDouble(result[i][3]), 
                            stringToBoolean(result[i][5]), stringToBoolean(result[i][6]), stringToBoolean(result[i][7]), 
                            stringToBoolean(result[i][8]), new ProductFamily(Integer.parseInt(result[i][9]), result[i][10]));            
        }
        
        return products;
    }
    
    public static Product getProductByID(int id) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        
        String[][] result = db.getFromDB("getFullProductList", "WHERE productID = "+id, "", "", 
                "productID", "productName", "purchasePrice", "salesPrice", "productSize", 
                "hasDecoration", "serviceProduct", "active", "sold", 
                "productFamilyID", "productFamilyName");
        
        db.closeDB();
        
        Product product = new Product(
                            Integer.parseInt(result[0][0]), result[0][1], result[0][4], Double.parseDouble(result[0][2]), Double.parseDouble(result[0][3]), 
                            stringToBoolean(result[0][5]), stringToBoolean(result[0][6]), stringToBoolean(result[0][7]), 
                            stringToBoolean(result[0][8]), new ProductFamily(Integer.parseInt(result[0][9]), result[0][10]));  
        
        return product;
    }
    
    private static boolean stringToBoolean(String str)
    {
        boolean answer = false;
        if (str.equals("1"))
        {
            answer = true;
        }        
        return answer;
    }
}